package com.leapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;


public class QuestaoSimplesActivity extends Activity {
	private LinearLayout l;
	private TextView enunciado;
	private RadioGroup radioGroup;
	private Context context;
	Questionario questionario =Questionario.getInstancia();
	private PassadorDeQuestao p = new PassadorDeQuestao();
	private Intent proxima_questao;
	private BdAdapter bdAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getBaseContext();
		//Recupera informações das questões
		final Bundle qinfo = getIntent().getExtras();
		
		radioGroup = new RadioGroup(this);
		enunciado = new TextView(this);
		l= new LinearLayout(this);
		l.setOrientation(LinearLayout.VERTICAL);
		l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		enunciado.setText(qinfo.getString("com.leapp.enunciado"));
		l.addView(enunciado);
		
		//Coloca as alternativas
		RadioButton aux;
		for(String item : qinfo.getStringArrayList("com.leapp.listaItens")){
			aux = new RadioButton(this);
			aux.setText(item);
			radioGroup.addView(aux);
		}
		l.addView(radioGroup);
		bdAdapter = new BdAdapter(this);

		//ClickListener que passa para a próxima questão
		OnCheckedChangeListener proxQuestaoListener = new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String enunciado = qinfo.getString("com.leapp.enunciado");
				int idx = group.getCheckedRadioButtonId();
				RadioButton rb = (RadioButton) findViewById(idx);
				String str = (String) rb.getText();
				bdAdapter.open();
				bdAdapter.criaQuestao(enunciado, str);
				bdAdapter.close();
				proxima_questao = p.mostrarProximaQuestao(context, qinfo.getInt("com.leapp.numero"));
				startActivity(proxima_questao);
			}
		};
		
		radioGroup.setOnCheckedChangeListener(proxQuestaoListener);
		
		setContentView(l);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.questao_simples, menu);
		return true;
	}
	
	
}
